import {defineStore} from 'pinia';
import http from "@/request/request";
import AxiosResponse from "axios";

interface MyType {
    [prop: string]: any
}

export const useUserInfo = defineStore('userinfo', {
    state: () => {
        return {
            id: "",
            name: "",
            username: "",
            loginTime: 0,
            token: "",
            touxiang: "",
            desc: "",

            // 登录页的背景图片
            loginbgimg:"",
            loginplacement:"right",
        }
    },
    actions: {
        // 登录完成后，注册用户信息
        denglu(userdata:MyType) {
            this.id = userdata["id"]
            this.name = userdata["name"]
            this.username = userdata["username"]
            this.loginTime = new Date().getTime()
            this.token = userdata["token"]
            this.touxiang = userdata["touxiang"]
            this.desc = userdata["desc"]
        },
        // 注销用户信息
        zhuxiao() {
            this.id = ""
            this.name = ""
            this.username = ""
            this.loginTime = 0
            this.token = ""
            this.touxiang = ""
            this.desc = ""
            http.anylogin().then((res:AxiosResponse) => {
                if (res.status === 200) {
                    console.log(res, "匿名登录的结果")
                    this.denglu(res.data.data)
                }
            })
        },
        changeloginbgimg(imgurl:string){
            this.loginbgimg = imgurl;
        },
        changeimgdesc(data:MyType){
            Object.keys(data).forEach((item:string)=>{
                // @ts-ignore
                this[item] = data[item]
            })
        },
        // 修改登录页配置
        changeloginset(key:string,value:string){
            // @ts-ignore
            this[key] = value
        },
        // 获取最新的token
        gettoken(){
            return this.token
        },
    },
    // pinia持久化
    persist: {
        key: 'kkkkyyy',    // 加密
        storage: window.sessionStorage, // 存储位置
    },
})
